package institution_service

import (
	"errors"

	"github.com/EDDYCJY/go-gin-example/models"
)

type GetTag struct {
	InstitutionID int
	PageNum       int
	PageSize      int
}

func (t *GetTag) GetTag() (interface{}, error) {
	maps := map[string]interface{}{
		"institution_id": t.InstitutionID,
	}
	total, _ := models.GetTagTotal(maps)
	goodsList, _ := models.GetTagPage([]string{"tag_id", "tag_name"}, maps, t.PageNum, t.PageSize, "")
	var lists []interface{}
	for _, v := range goodsList {
		params := map[string]interface{}{
			"tag_id":   v.TagID,
			"tag_name": v.TagName,
		}
		lists = append(lists, params)
	}
	return map[string]interface{}{
		"lists": lists,
		"total": total,
	}, nil
}

// Login 定义登录请求结构体
type AddTag struct {
	InstitutionID int
	TagName       string
}

// AddTag 执行用户登录逻辑
func (t *AddTag) AddTag() (map[string]interface{}, error) {
	var maps = map[string]interface{}{
		"institution_id": t.InstitutionID,
		"tag_name":       t.TagName,
	}
	tagInfo, _ := models.GetTagInfo([]string{"tag_id"}, maps)
	if tagInfo.TagID != 0 {
		return nil, errors.New("标签已存在")
	}
	_, err := models.AddTag(maps)
	if err != nil {
		return nil, err
	}
	return nil, nil
}

// Login 定义登录请求结构体
type EditTag struct {
	TagID         int
	InstitutionID int
	TagName       string
}

// EditTag 执行用户登录逻辑
func (t *EditTag) EditTag() (map[string]interface{}, error) {

	var maps = map[string]interface{}{
		"institution_id": t.InstitutionID,
		"tag_id":         t.TagID,
	}
	var update = map[string]interface{}{
		"tag_name": t.TagName,
	}
	err := models.EditTag(maps, update)
	if err != nil {
		return nil, err
	}
	return nil, nil
}

// Login 定义登录请求结构体
type DeleteTag struct {
	TagID         int
	InstitutionID int
}

// DeleteTag 执行用户登录逻辑
func (t *DeleteTag) DeleteTag() (map[string]interface{}, error) {
	var maps = map[string]interface{}{
		"institution_id": t.InstitutionID,
		"tag_id":         t.TagID,
	}
	err := models.CleanTag(maps)
	if err != nil {
		return nil, err
	}
	return nil, nil
}
